<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Menu extends Controller_Admin_Component
{

    private $menu;

    public function before()
    {
        parent::before();
        $this->menu = Model::factory('menu');
        I18n::set_current_file('menu');
    }

    public function action_add()
    {
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['url'] = Arr::get($_POST, 'url', '');
            $data['type'] = Arr::get($_POST, 'type', '');
            $data['position'] = Arr::get($_POST, 'position', 999);
            $data['active'] = Arr::get($_POST, 'action', 1);
            if ($data['name'] == '')
            {
                Message::error('请填写导航的名称');
                $this->request->redirect('admin/menu/add');
            }
            if ($this->menu->insert($data))
            {
                Message::success(__('add success'));
                $this->request->redirect('admin/menu/add');
            }
        }
        $this->template->view('component_menu_add');
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        $detail = $this->menu->get_menu_detail($id);
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['url'] = Arr::get($_POST, 'url', '');
            $data['type'] = Arr::get($_POST, 'type', '');
            $data['position'] = Arr::get($_POST, 'position', 999);
            $data['active'] = Arr::get($_POST, 'action', 1);
            if ($data['name'] == '')
            {
                Message::error('请填写导航的名称');
                $this->request->redirect('admin/menu/edit/' . $id);
            }
            if ($this->menu->update($data, array('id' => $id)))
            {
                Message::success(__('update success'));
                $this->request->redirect('admin/menu/edit/' . $id);
            }
        }
        $this->template->assign('data', $detail);
        $this->template->view('component_menu_edit');
    }

    public function action_list()
    {
        $this->display_list($this->menu->get_menu_list());
    }

    public function display_list($menu_list)
    {
        $this->template->assign('menu_list', $menu_list['list']);
        $this->template->assign('page', $menu_list['page']);
        $this->template->view('component_menu_list');
    }

    public function action_createfile()
    {
        $menu_list_temp = Model::factory('menu')->get_results('order by position');
        $menu_list = array();
        if (Arr::validate_arr($menu_list_temp))
        {
            foreach ($menu_list_temp as $key => $value)
            {
                $menu_list[$value['type']][] = $value;
            }
        }

        $file_path = SYSTEM_MENU_CACHE_PATH . 'menu.php';
        if (Cache::instance()->write($file_path, $menu_list))
        {
            Message::success('写入成功');
            $this->request->redirect(URL::site('admin/menu/list'));
        }
    }

}